Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.IO
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Diagnostics


' This window is responsible for displaying the tray icon, responding to
' menu commands from the user, and initializing the p2p channel used
' for sharing pictures.
' Note: this Window is hidden - and a Forms user control provides
' the tray icon and context menu functionality.
Partial Public Class TrayWindow
    Inherits System.Windows.Window
    Private Const TrayMessage As String = "Shares pictures using Windows Peer to Peer technology"
    Private Const PublishMessage As String = "Publishing shared pictures..."

    ' User control that shows the tray icon, shows context menu.
    Private _nIcon As NotifyIconControl

    ' Works with WCF and PeerChannel to send and receive pictures.
    Private _peerChannel As PictureSharePeerChannel = Nothing

    Public Sub New()
        ' VS designer generated code.
        InitializeComponent()

        ' Create, display, and register for events from the notify
        ' icon user control.
        InitializeTrayApp()

        Me.WindowState = WindowState.Minimized

        AddHandler Loaded, AddressOf TrayWindow_Loaded
        AddHandler Closed, AddressOf TrayWindow_Closed
    End Sub

    Private Sub TrayWindow_Closed(ByVal sender As Object, ByVal e As EventArgs)
        Cleanup()
    End Sub

    ' Called when the user selects the Exit context menu item.
    Private Sub nIcon_ExitSelected(ByVal sender As Object, ByVal e As EventArgs)
        Cleanup()
    End Sub

    Private Sub Cleanup()
        If Not Me._nIcon Is Nothing Then
            Me._nIcon.Dispose()
        End If
        If Not Me._peerChannel Is Nothing Then
            Me._peerChannel.Close()
        End If

        Application.Current.Shutdown()
    End Sub

    Private Sub TrayWindow_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.Visibility = Visibility.Hidden

        Try
            ' Create and connect to the picture sharing peer channel.
            Me._peerChannel = New PictureSharePeerChannel()
            Me._peerChannel.Connect()
        Catch ex As InvalidOperationException
            MessageBox.Show(ex.Message, "Peer-to-peer Error", MessageBoxButton.OK, MessageBoxImage.Error)
            Me.Close()
        End Try
    End Sub

    Private Sub InitializeTrayApp()
        ' Create a new notify icon user control instance.
        Me._nIcon = New NotifyIconControl()

        ' Register for event notifications when the user wants
        ' to show the configuration window, publish pictures, or
        ' exit the application.

        AddHandler _nIcon.ShowConfigWindowSelected, AddressOf nIcon_ShowConfigWindowSelected
        AddHandler _nIcon.PublishSelected, AddressOf nIcon_PublishSelected
        AddHandler _nIcon.OpenInFolder, AddressOf _nIcon_OpenInFolder
        AddHandler _nIcon.OpenOutFolder, AddressOf _nIcon_OpenOutFolder
        AddHandler _nIcon.ExitSelected, AddressOf nIcon_ExitSelected

        ' Set the default message to display to the user during mouseover.
        Me._nIcon.TrayText = TrayMessage
    End Sub

    Private Sub _nIcon_OpenOutFolder(ByVal sender As Object, ByVal e As EventArgs)
        Dim psi As ProcessStartInfo = New ProcessStartInfo("explorer.exe", SettingsWrapper.ShareFolder)
        Process.Start(psi)
    End Sub

    Private Sub _nIcon_OpenInFolder(ByVal sender As Object, ByVal e As EventArgs)
        Dim psi As ProcessStartInfo = New ProcessStartInfo("explorer.exe", SettingsWrapper.CacheFolder)
        Process.Start(psi)
    End Sub


    ' Called when the user selects the Publish context menu item.
    Private Sub nIcon_PublishSelected(ByVal sender As Object, ByVal e As EventArgs)
        Me._nIcon.TrayText = PublishMessage

        ' Publish the pictures in the configured share folder by sending
        ' them to the configured peer channel.
        ' Note: this could be updated to be perfomred on a different thread to
        ' prevent blocking.
        For Each picFile As String In Directory.GetFiles(SettingsWrapper.ShareFolder)
            Me._peerChannel.SendPicture(picFile)
        Next picFile

        Me._nIcon.TrayText = TrayMessage
    End Sub

    ' Called when the user indicates a desire to change the configuration.
    Private Sub nIcon_ShowConfigWindowSelected(ByVal sender As Object, ByVal e As EventArgs)
        ' Note: the configuration window only makes modifications to the
        ' application user config file.
        Dim configWin As ConfigurationWindow = New ConfigurationWindow()
        configWin.Show()
    End Sub


End Class

